package net.lainiao.portal.control;
import com.github.pagehelper.PageInfo;
import net.lainiao.base.search.ResultModel;
import net.lainiao.service.elastic.ElasticService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/1/17.
 */
@Controller
public class HomeControl {
    @Resource
    private ElasticService elasticService;
    @RequestMapping(value="/")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/search")
    public String search(@RequestParam(value = "s",defaultValue = " ") String s, ModelMap modelMap,@RequestParam(value="index",defaultValue = "1") String index){
        if(s.length()>255){
            s=s.substring(0,254);
        }
        int pageIndex=Integer.parseInt(index);
        PageInfo<ResultModel> pageInfo=elasticService.search(pageIndex,10,s);
        modelMap.put("s",s);
        modelMap.put("models",pageInfo.getList());
        modelMap.put("pageIndex",pageIndex);
        modelMap.put("pageTotal",pageInfo.getPages());
        return "search";
    }
}
